package cn.bqjr.eily.samples.controller.common;

import cn.bqjr.eily.mybatis.spring.boot.utils.PagingUtils;
import cn.bqjr.eily.samples.controller.base.BaseController;
import cn.bqjr.eily.samples.entity.po.Dict;
import cn.bqjr.eily.samples.service.base.IDictService;
import cn.bqjr.eily.web.modules.apiversion.ApiVersion;
import cn.bqjr.eily.web.utils.AssertUtils;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("api/{version}/dicts")
@ApiVersion(1)
public class DictController extends BaseController {

    @Autowired
    private IDictService dictService;

    /**
     * 新增字典记录
     *
     * @param dict
     * @return
     */
    @PostMapping
    public Object addDict(@RequestBody Dict dict) {
        return dictService.insert(dict);
    }

    /**
     * 删除字典记录
     *
     * @param dictId
     * @return
     */
    @DeleteMapping(value = "/{dictId}")
    public Object deleteDict(@PathVariable("dictId") Long dictId) {
        return dictService.deleteById(dictId);
    }

    /**
     * 批量删除字典记录
     *
     * @param dictIds
     * @return
     */
    @DeleteMapping
    public Object deleteDictByBatchId(@RequestParam("ids") List<Long> dictIds) {
        return dictService.deleteBatchIds(dictIds);
    }

    /**
     * 更新字典记录
     *
     * @param dictId
     * @param dict
     * @return
     */
    @PutMapping(value = "/{dictId}")
    public Object updateDict(@PathVariable("dictId") Long dictId,
                             @RequestBody Dict dict) {
        AssertUtils.equals(dictId, dict.getDictId());
        return dictService.updateById(dict);
    }

    /**
     * 查询指定字典记录
     *
     * @param dictId
     * @return
     */
    @GetMapping(value = "/{dictId}")
    public Object selectDict(@PathVariable("dictId") Long dictId) {
        return dictService.selectById(dictId);
    }

    /**
     * 查询字段列表
     *
     * @return
     */
    @GetMapping()
    public Object selectDictList() {
        PagingUtils.pagingIfExist(request);
        return dictService.selectList(new EntityWrapper<Dict>());
    }
}
